﻿using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using com.fukefu.source;
using com.fukefu.sql;

namespace com.fukefu.info.b2b.auto
{
    public class YouBoy : Information
    {
        //所有图片
        List<string> list_img = new List<string>();
        HashSet<string> hs_url = new HashSet<string>();
        string login_name = "";
        //登陆
        public override bool Login(string name, string pass, string vcode)
        {
            ParametersData pd = new ParametersData();
            pd.Add("user", name);
            pd.Add("pass", pass);
            pd.Add("check", "");
            pd.Add("cnt", "1");
            pd.Add("loginSta", "true");
            if (source.Post("http://my.youboy.com/UserInfoServlet?op=login", pd.Read(), "http://my.youboy.com/signin.jsp").Trim().Equals("loginSuccess"))
            {
                this.login_name = name;
                LoadImages();
                LoadArticle();
                return true;
            }
            return false;
        }
        //发布文章
        public override InformationResult PostArticle(string title, string content, string vcode)
        {
            InformationResult info = new InformationResult();
            //标题验证
            source.Get("http://my.youboy.com/shangpu/kwshow.jsp?kw=" + MyFunction.EncodeByUtf8(title) + "&&sta=0", "http://my.youboy.com/shangpu/gfb.jsp");
            string html = "", tt = "", intro = "";
            tt = Regex.Replace(content, "<.+?>", "");
            tt = Regex.Replace(tt, "\\s+", "");
            //处理描述信息
            if (tt.Length > (80 - title.Length))
            {
                intro = tt.Substring(random.Next(tt.Length - (80 - title.Length)), 80 - title.Length) + "...";
            }
            else
            {
                intro = tt;
            }
            intro = title + "," + intro;
            ParametersData pd = new ParametersData("gb2312");
            pd.Add("id", "");
            pd.Add("gdate", (MyFunction.GetTimestampNow() - 945000).ToString());
            pd.Add("nkw", "");
            pd.Add("kw", title);
            pd.Add("sid", "1");
            pd.Add("fee", pif.Get("产品价格") + "元/台");
            pd.Add("Input", "");
            pd.Add("title", title);
            pd.Add("htmlintro", content);
            pd.Add("tt", tt);
            pd.Add("intro", intro);
            pd.Add(GetRandomImagesCode());
            html = source.Post("http://my.youboy.com/shangpu/gfb_do.jsp", pd.Read(), "http://my.youboy.com/shangpu/gfb.jsp");
            if (html.IndexOf("fboktxt") != -1)
            {
                info.Code = 1;
                info.Content = GetAllArticle();
            }
            else if (html.IndexOf("text/javascript") != -1)
            {
                info.Content = Regex.Match(html, "alert\\('([^\']+)'\\)").Groups[1].Value;
            }
            else if (hs_url.Count > 800)
            {
                info.Code = 2;
            }
            return info;
        }

        private string GetAllArticle()
        {
            string result = "";
            string html = source.Get("http://my.youboy.com/shangpu/ggl.jsp");
            html = Regex.Replace(html, "\\s+", "");
            html = Regex.Replace(html, "&[^;]+;", "");
            html = Regex.Replace(html, "<em>|</em>", "");
            MatchCollection mc = Regex.Matches(html, "\"pro_right_title\"><Ahref=\"[^\"]+show0sp([0-9]+\\.html)\"");
            foreach (Match m in mc)
            {
                GroupCollection gc = m.Groups;
                //只抓取第一条记录
                if (!hs_url.Contains("http://www.youboy.com/s" + gc[1].Value))
                {
                    //一级域名
                    hs_url.Add("http://www.youboy.com/s" + gc[1].Value);
                    result += "|http://www.youboy.com/s" + gc[1].Value;
                }
                if (!hs_url.Contains("http://b2b.youboy.com/show0cp" + gc[1].Value))
                {
                    //二级域名
                    hs_url.Add("http://b2b.youboy.com/show0cp" + gc[1].Value);
                    result += "|http://b2b.youboy.com/show0cp" + gc[1].Value;
                }
                if (!hs_url.Contains("http://" + login_name + ".b2b.youboy.com/show0sp" + gc[1].Value))
                {
                    //三级域名
                    hs_url.Add("http://" + login_name + ".b2b.youboy.com/show0sp" + gc[1].Value);
                    result += "|http://" + login_name + ".b2b.youboy.com/show0sp" + gc[1].Value;
                }
            }
            return result;
        }
        //删除文章
        private bool DeleteArticle(string id)
        {
            string html = source.Get("http://my.youboy.com/shangpu/gdel.jsp?gkwid=" + id + "&sta=1", "http://my.youboy.com/shangpu/ggl.jsp");
            if (html.IndexOf("删除成功") != -1)
            {
                return true;
            }
            return false;
        }
        //随机图片
        private string GetRandomImagesCode()
        {
            string result = "";
            int[] sf = MyFunction.GetDifferentRandomNum(list_img.Count, list_img.Count > 3 ? 4 : list_img.Count);
            foreach (int i in sf)
            {
                result += "&smallImg=" + MyFunction.EncodeByGb2312(list_img[i]) + "&bigImg=" + MyFunction.EncodeByGb2312(list_img[i].Replace("s", ""));
            }
            return result;
        }
        //加载所有文章
        private void LoadArticle()
        {
            SqlServerOperate sql = new SqlServerOperate();
            SqlDataReader re;
            re = sql.Query("select a.url,b.[user] from  已发布 as a,_site_账户 as b where b.[user]='" + login_name + "' and a.uid=b.uid");
            while (re.Read())
            {
                hs_url.Add(re["url"].ToString());
            }
            re.Close();
            sql.Close();
        }
        //加载所有图片
        private void LoadImages()
        {
            string html;
            int page = 1;
            do
            {
                html = source.Post("http://my.youboy.com/shangpu/alumn.jsp?asid=0&page=" + page, "", "", "http://my.youboy.com/shangpu/gfb.jsp");
                MatchCollection mc = Regex.Matches(html, "<div\\s+class=\"ht_img_con\"><img\\s+src=\"([^\"]+)\"\\s+[^>]+></div>\\s+<div[^>]+>([^<]+)</div>");
                foreach (Match m in mc)
                {
                    list_img.Add(m.Groups[1].Value);
                }
                page++;
            } while (Regex.IsMatch(html, "page=" + page + "&asid=0'> 下一页 </a>"));
        }
    }
}
